package com.fengfeng.sdk;

import android.app.Activity;
import android.widget.Toast;

import com.fengfeng.http.HttpCallback;
import com.fengfeng.http.HttpUrlConstant;
import com.fengfeng.http.HttpUtils;
import com.fengfeng.sdk.login.LoginManager;
import com.fengfeng.utils.FFLogUtils;
import com.fengfeng.utils.FFToastUtils;
import com.fengfeng.view.dialog.ExitDialog;

import java.util.HashMap;

import org.json.JSONObject;

/**
 * @author xiaoguokeng
 * 该类的作用就是对外提供给研发调用接口和设置监听
 * @Date 2024/8/7
 */
public class FengFengSDK {

    private static FengFengSDK mInstance;
    private boolean isInit = false;
    private SDKCallback mSDKCallback;
    private Activity mActivity;

    private FengFengSDK() {
    }

    //单例模式，获取唯一的SDK对象，单例模式，就是用来保证只有一个对象产生。
    public static FengFengSDK getInstance() {
        if (mInstance == null) {
            mInstance = new FengFengSDK();
        }
        return mInstance;
    }

    /**
     * 初始化接口
     */
    public void init(SDKCallback callback) {
        if (callback == null) {
            FFToastUtils.getInstance().showToast("请先初始化");
        }
        if (isInit) {
            callback.initFail(-1, "请勿重复初始化");
            return;
        }
        mSDKCallback = callback;
        //这一块就是调用初始化接口，并且把成功还是失败，返回给研发
        HashMap<String, Object> data = new HashMap<>();
        data.put("gid", "1001");
        data.put("pid", "1");
        HttpUtils.doPost(HttpUrlConstant.INIT_URL, data, new HttpCallback() {
            @Override
            public void success(JSONObject data, String msg) {
                isInit = true;
                //给管理类传递回调
                LoginManager.getInstance().setCallback(mSDKCallback);
                mSDKCallback.initSuccess();
            }

            @Override
            public void error(int code, String msg) {
                mSDKCallback.initFail(code, msg);
            }
        });
    }

    /**
     * 登录接口
     */
    public void login() {
        if (!isInit) {
            FFToastUtils.getInstance().showToast("请先初始化");
            return;
        }
        LoginManager.getInstance().login(mActivity);
    }

    /**
     * 账号登出接口
     */
    public void logout() {
        if (!isInit) {
            FFToastUtils.getInstance().showToast("请先初始化");
            return;
        }
        LoginManager.getInstance().logout();
    }

    public void onCreate(Activity activity) {
        mActivity = activity;
        FFToastUtils.getInstance().setActivity(mActivity);
    }

    public void onDestroy() {

    }

    public void onResume() {

    }

    public void onRestart() {

    }

    public void onBackPressed() {
        if (mActivity == null) {
            FFLogUtils.i("没有接入onCreate方法，请先接入onCreate方法");
            return;
        }
        ExitDialog exitDialog = new ExitDialog(mActivity);
        exitDialog.show();
    }

}
